package com.ch.web.controller;

import com.ch.web.entity.user.XyCommentEntity;
import com.ch.web.service.XyCommentService;
import com.ch.web.utils.ConstUtils;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * @author chenHao
 * @email 1429542285@qq.com
 * @date 2022-10-15 13:58:33
 */
@RestController
@RequestMapping("xy/comment")
public class XyCommentController {
    @Autowired
    private XyCommentService xyCommentService;

    /**
     * 列表
     */
    @RequestMapping("/list")

    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = xyCommentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")

    public R info(@PathVariable("id") Long id) {
        XyCommentEntity xyComment = xyCommentService.getById(id);
        return R.ok().put("xyComment", xyComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody XyCommentEntity xyComment) {
        xyCommentService.save(xyComment);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody XyCommentEntity xyComment) {
        xyCommentService.updateById(xyComment);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        xyCommentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 添加评论
     */
    @PostMapping("/add")
    public R addComment(@RequestBody XyCommentEntity xyCommentEntity, HttpServletRequest request) {
        Long commentId = xyCommentService.saveComment(xyCommentEntity, request);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", xyCommentEntity.getId());
        map.put("createTime", ConstUtils.getCurrentTime());
        map.put("commentId",commentId);
        return R.ok(map);
    }
}
